﻿
//#region Cookies读写
var Cookies = {};
/**//**
 * 设置Cookies
 */
Cookies.set = function (name, value) {
    var argv = arguments;
    var argc = arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : '/';
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape(value) +
       ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
       ((path == null) ? "" : ("; path=" + path)) +
       ((domain == null) ? "" : ("; domain=" + domain)) +
       ((secure == true) ? "; secure" : "");
};
/**//**
 * 读取Cookies
 */
Cookies.get = function (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    var j = 0;
    while (i < clen) {
        j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return Cookies.getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0)
            break;
    }
    return null;
};
/**//**
 * 清除Cookies
 */
Cookies.clear = function (name) {
    if (Cookies.get(name)) {
        var expdate = new Date();
        expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
        Cookies.set(name, "", expdate);
    }
};

Cookies.getCookieVal = function (offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
};
//#endregion

//#region Js按需加载
function JsLoader() {
    this.load = function (url) {
        //获取所有的<script>标记
        var ss = document.getElementsByTagName("script");
        //判断指定的文件是否已经包含，如果已包含则触发onsuccess事件并返回
        for (i = 0; i < ss.length; i++) {
            if (ss[i].src && ss[i].src.indexOf(url) != -1) {
                this.onsuccess();
                return;
            }
        }
        //创建script结点,并将其属性设为外联JavaScript文件
        s = document.createElement("script");
        s.type = "text/javascript";
        s.src = url;
        //获取head结点，并将<script>插入到其中
        var head = document.getElementsByTagName("head")[0];
        head.appendChild(s);

        //获取自身的引用
        var self = this;
        //对于IE浏览器，使用readystatechange事件判断是否载入成功
        //对于其他浏览器，使用onload事件判断载入是否成功
        s.onload = s.onreadystatechange = function () {
            //在此函数中this指针指的是s结点对象，而不是JsLoader实例,
            //所以必须用self来调用onsuccess事件，下同。
            if (this.readyState && this.readyState == "loading") return;
            self.onsuccess();
        }
        s.onerror = function () {
            head.removeChild(s);
            self.onfailure();
        }
    };
    //定义载入成功事件
    this.onsuccess = function () { };
    //定义失败事件
    this.onfailure = function () { };
}
//创建对象
var jsLoader = new JsLoader();
//定义载入失败处理程序
jsLoader.onfailure = function () {
    Ext.Msg.alert("Error", "文件载入失败！");
}
//#endregion

//Extjs4 树形下拉 模型
Ext.define('TreeModel', {
    extend: 'Ext.data.Model',
    fields: [
        {
            name: 'id'
        },
        {
            name: 'text'
        },
        {
            name: 'cls'
        },
        {
            name: 'leaf'
        }
    ]
    });

//在线打卡
function cardOnline() {
    Ext.Msg.show({
        title: '在线打卡',
        msg: '提示:您确定在线打卡吗?',
        buttons: Ext.Msg.YESNO,
        fn: function (btn) {
            if (btn == 'yes') {
                Ext.Ajax.request({
                    url: '/Card.mvc/CardOnline',
                    method: 'POST',
                    success: function (response, options) {
                        var responseMessage = Ext.JSON.decode(response.responseText);
                        if (responseMessage.Result) {
                            Ext.Msg.alert('打卡成功', "您的打卡时间是:" + responseMessage.Message);
                        } else {
                            Ext.MessageBox.show({
                                title: "打卡失败", msg: responseMessage.Message
                            });
                        }
                    },
                    failure: function (response, options) {
                        var responseMessage = Ext.JSON.decode(response.responseText);
                        Ext.MessageBox.show({
                            title: "打卡失败", msg: responseMessage.Message
                        });
                    }
                });
            }
        }
    });
}

//登出
function loginOut() {
    Ext.Msg.show({
        title: '退出系统',
        msg: '提示:退出系统前请注意保存数据,确定要退出吗?',
        buttons: Ext.Msg.YESNO,
        fn: function (btn) {
            if (btn == 'yes') {
                Ext.Ajax.request({
                    url: '/EmpProfile.mvc/Logout',
                    method: 'POST',
                    success: function () {
                        Cookies.clear("empId");
                        Cookies.clear("empName");
                        Cookies.clear("jobNo");
                        Cookies.clear("empBasicInfo");
                        window.location = "Login.aspx?ReturnUrl=Index.aspx";
                    }
                });
            }
        }
    });
    return false;
}